package app.ui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.text.NumberFormat;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.NumberFormatter;

import app.action.AppAction;

public class MainFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1375545582422380851L;

	private JPanel jPanel = null;
	private JPanel jPanel2 = null;
	private JFormattedTextField[] jTextFields = null;
	private JLabel[] jLabels = null;
	private JButton jButton = null;
	private JButton jButton2 = null;
	private JCheckBox jCheckBox = null;
	private NumberFormatter format;

	public MainFrame() {
		super("Iterest Installment:");

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setBounds(800, 100, 380, 350);
		setResizable(false);
		getContentPane().setLayout(new GridBagLayout());

		initialize();
		
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 1;
		c.weighty = 1;
		c.fill = GridBagConstraints.BOTH;
		add(jPanel, c);
		
		c.gridy = 1;
		c.weighty = 0.2;
		add(jPanel2, c);
	}

	private void initialize() {
		String[] labelTexts = { "Price", "Prio - Pay %", "Interest %",
				"Months", "Type", "Prio - Pay", "OWE", "Each month - Pay",
				"Interest Installment", "Total - Pay" };
		int num = labelTexts.length;
		jPanel = new JPanel(new GridLayout(num, 2));
		jPanel.setBackground(Color.WHITE);
		jLabels = new JLabel[num];
		jTextFields = new JFormattedTextField[num];

		AppAction action = new AppAction(this);
		format = new NumberFormatter(NumberFormat.getInstance());
		format.setValueClass(Float.class);
		format.setMinimum(0F);
		format.setMaximum(Float.MAX_VALUE);
		format.setCommitsOnValidEdit(true);
		
		for (int i = 0; i < num; i++) {
			if(i < labelTexts.length) {
				jLabels[i] = new JLabel(" " + labelTexts[i]);
			} else {
				jLabels[i] = new JLabel("Label " + i);
			}
			
			jPanel.add(jLabels[i]);

			jTextFields[i] = new JFormattedTextField(format);
			
			jTextFields[i].addActionListener(action);
			jTextFields[i].addFocusListener(action);
			
			if(i > 3) {
				jTextFields[i].setEditable(false);
				jTextFields[i].setFocusable(false);
//				jTextFields[i].setEnabled(false);
			}
			jPanel.add(jTextFields[i]);
		}
		jTextFields[0].setValue(3840F);
		jTextFields[1].setValue(10F);
		jTextFields[2].setValue(2.6F);
		jTextFields[3].setValue(6F);
		jTextFields[4].setToolTipText("Type of interest: Compound or Simple");
		jTextFields[8].setBackground(Color.YELLOW);
		
		jPanel2 = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		c.gridx = 0; c.gridy = 0;
		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.PAGE_START;
		c.gridheight = 2;
		c.weightx = 1;
		
		jCheckBox = new JCheckBox("auto add \'000\'");
		jButton = new JButton("Calculate 1.");
		jButton2 = new JButton("Calculate 2.");
		
		jCheckBox.setSelected(action.isMultiple1000());
		jCheckBox.addActionListener(action);
		jCheckBox.setToolTipText("Auto multiple with 1000 when the inputted " +
				"price smaller than a million");
		
		jButton.addActionListener(action);
		jButton.setToolTipText("Caculating for type 1: simple interest");
		jButton2.addActionListener(action);
		jButton2.setToolTipText("Caculating for type 2: compound interest");
		
		jPanel2.add(jCheckBox, c);

		c.gridheight = 1;
		c.gridx = 1;
		c.insets = new Insets(2, 0, 0, 0);
		jPanel2.add(jButton, c);
		
		c.insets = new Insets(0, 0, 2, 0);
		c.anchor = GridBagConstraints.PAGE_END;
		c.gridy = 1;
		jPanel2.add(jButton2, c);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MainFrame mainFrame = new MainFrame();

		mainFrame.setVisible(true);
	}

	public JFormattedTextField[] getjTextFields() {
		return jTextFields;
	}

	public NumberFormatter getFormat() {
		return format;
	}

	public void setFormat(NumberFormatter format) {
		this.format = format;
	}

	public JCheckBox getjCheckBox() {
		return jCheckBox;
	}

}
